﻿using System;
using System.Web.UI.WebControls;
using CustomerContactSystem.Core.Model;

namespace CustomerContactSystem.WebForms.Enquiries.Tags {
    public partial class Manage : BasePage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsValidPageRequest(true, true, "id")) {
                return;
            }

            if (Request.QueryString["remove"] != null) {
                RemoveTag();
                Response.Redirect("~/Enquiries/Tags/Manage.aspx?id=" + Request.QueryString["id"]);
                Response.End();
            }

            EnquiryIdCaption.Text = Request.QueryString["id"];
            EnquiryIdCaption.NavigateUrl = "~/Enquiries/View.aspx?id=" + Request.QueryString["id"];
            GetTags();
            GetEnquiryTags();
        }

        private void RemoveTag() {
            var removeId = int.Parse(Request.QueryString["remove"]);
            var removeTag = TagRepository.GetById(removeId);
            var enquiryId = int.Parse(Request.QueryString["id"]);
            var enquiry = EnquiryRepository.GetById(enquiryId);

            enquiry.RemoveTag(LoggedUser.DisplayName, removeTag);
            
            EnquiryRepository.Update(enquiry);
        }

        private void GetTags() {
            var tags = TagRepository.QueryOver().OrderBy(t => t.TagName).Asc.List();
            BindDropDownList(Tags, tags, "TagName", "Id", "--Select--", "0");
        }

        private void GetEnquiryTags() {
            var enquiryId = int.Parse(Request.QueryString["id"]);
            var enquiry = EnquiryRepository.GetById(enquiryId);

            TagsList.DataSource = enquiry.Tags;
            TagsList.DataBind();
        }

        protected void TagsListItemDataBound(object sender, RepeaterItemEventArgs e) {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) {
                return;
            }

            var tag = (Tag)e.Item.DataItem;
            var removeLink = (HyperLink)e.Item.FindControl("RemoveLink");

            removeLink.NavigateUrl = "~/Enquiries/Tags/Manage.aspx?id=" + Request.QueryString["id"] + "&remove=" + tag.Id;
        }

        protected void AddSelectedTagClick(object sender, EventArgs e) {
            if (!IsValidPageRequest(true, false, "id")) {
                return;
            }

            var enquiryId = int.Parse(Request.QueryString["id"]);
            var enquiry = EnquiryRepository.GetById(enquiryId);

            if (enquiry == null) {
                return;
            }

            if (Tags.SelectedIndex > 0) {
                var tag = TagRepository.GetById(int.Parse(Tags.SelectedItem.Value));
                enquiry.AddTag(LoggedUser.DisplayName, tag);                
            }

            if (TagName.Text.Trim().Length > 0) {
                var existingTag = TagRepository.QueryOver().Where(t => t.TagName == TagName.Text.Trim()).SingleOrDefault();
                if (existingTag == null) {
                    existingTag = new Tag
                        {
                            TagName = TagName.Text.Trim()
                        };
                    TagRepository.Add(existingTag);
                }
                enquiry.AddTag(LoggedUser.DisplayName, existingTag);                
            }

            EnquiryRepository.Update(enquiry);
            Response.Redirect("~/Enquiries/Tags/Manage.aspx?id=" + enquiry.Id);
        }
    }
}